package com.heima.search.service.Impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.search.pojo.ApUserSearch;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class ApUserSearchServiceImpl implements ApUserSearchService {


    @Autowired
    private MongoTemplate template;
    @Override
    @Async
    public void insert(String searchWords, Integer id) {

//        Query query = Query.query(Criteria.where("userId").is(id).and("keyword").is(searchWords));
//
//        ApUserSearch one = template.findOne(query, ApUserSearch.class);
//
//        if (one!=null){
//            one.setCreatedTime(new Date());
//            template.save(one);
//            return;
//        }
//
//
//        ApUserSearch apUserSearch = new ApUserSearch();
//        apUserSearch.setUserId(id);
//        apUserSearch.setKeyword(searchWords);
//        apUserSearch.setCreatedTime(new Date());
//
//
//        Query userId = Query.query(Criteria.where("userId").is(id));
//        userId.with(Sort.by(Sort.Direction.DESC,"createTime"));
//        List<ApUserSearch> apUserSearches = template.find(userId, ApUserSearch.class);
//
//        if (apUserSearches.size()<10&&apUserSearches!=null){
//            template.save(apUserSearch);
//        }else {
//            ApUserSearch apUserSearch1 = apUserSearches.get(apUserSearches.size() - 1);
//            Query query1 = Query.query(Criteria.where("userId").is(apUserSearch1.getId()));
//            template.findAndReplace(query1,apUserSearch1);
//        }

        //如果存在id
        Query query = Query.query(Criteria.where("userId").is(id).and("keyword").is(searchWords));
        ApUserSearch one = template.findOne(query, ApUserSearch.class);
        if (one!=null){
            one.setCreatedTime(new Date());
            one.setKeyword(searchWords);
            template.save(one);
            return;
        }


        //如果不存在id

        ApUserSearch apUserSearch = new ApUserSearch();
        apUserSearch.setUserId(id);
        apUserSearch.setKeyword(searchWords);
        apUserSearch.setCreatedTime(new Date());


        Query query1 = Query.query(Criteria.where("userId").is(id));
        query1.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<ApUserSearch> apUserSearches = template.find(query1, ApUserSearch.class);

        if (apUserSearches.size()<10||apUserSearches==null){
            template.save(apUserSearch);
        }else{
            ApUserSearch apUserSearch1 = apUserSearches.get(apUserSearches.size() - 1);
            template.findAndReplace(Query.query(Criteria.where("userId").is(apUserSearch.getId())),apUserSearch1);
        }

    }

    @Override
    public ResponseResult load() {

        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Query userId = Query.query(Criteria.where("userId").is(AppThreadLocalUtil.getUser().getId()));
        userId.with(Sort.by(Sort.Direction.DESC,"createTime"));

        List<ApUserSearch> apUserSearches = template.find(userId, ApUserSearch.class);
        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delRecord(HistorySearchDto dto) {

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Query query = new Query(Criteria.where("id").is(dto.getId()).and("userId").is(user.getId()));
        template.remove(query,ApUserSearch.class);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
